package com.mypage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.cloudcc.mobile.R;
import com.litesuits.android.log.Log;
import com.mypage.bean.CheckInBean;
import com.mypage.bean.CreateGoupPersonBean;
import com.mypage.view.activity.beautMore.BusinCreateGroupPerson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by liuyongjie on 2017/5/11.
 */

public class NewHistoryCheckInAdapter extends BaseAdapter {

    public List<CheckInBean.Locations> mList;
    public final Context context;
    public String search;

    public NewHistoryCheckInAdapter(final List<CheckInBean.Locations> mQList, Context context, String search) {
        this.mList = mQList;
        this.context = context;
        this.search = search;
    }

    public void addData(final List<CheckInBean.Locations> data) {
        if (mList == null) {
            mList = new ArrayList<CheckInBean.Locations>();
        }
        mList.addAll(data);
        notifyDataSetChanged();
        Log.d("istrue", "过来了");
    }

    @Override
    public int getCount() {
        return mList.size() == 0 ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
       if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.checkin_list_item, null);

            viewHolder.topTimeLine = (TextView) convertView.findViewById(R.id.topTimeLine);




            //上
            viewHolder.sbcontent = (TextView) convertView.findViewById(R.id.sbcontext);
            viewHolder.sbtimes = (TextView) convertView.findViewById(R.id.sbtime);
            viewHolder.sbworkName = (TextView) convertView.findViewById(R.id.sbworkName);
//            viewHolder.sbLayout = (RelativeLayout) convertView.findViewById(R.id.sbLayout);
//            viewHolder.sbData = (TextView) convertView.findViewById(R.id.sbData);


//中
            viewHolder.zwcontent = (TextView) convertView.findViewById(R.id.zwcontext);
            viewHolder.zwtimes = (TextView) convertView.findViewById(R.id.zwtime);
            viewHolder.zwworkName = (TextView) convertView.findViewById(R.id.zwworkName);
//            viewHolder.zwLayout = (RelativeLayout) convertView.findViewById(R.id.zwLayout);
//            viewHolder.zwData = (TextView) convertView.findViewById(R.id.zwData);

//下
            viewHolder.xbcontent = (TextView) convertView.findViewById(R.id.xbcontext);
            viewHolder.xbtimes = (TextView) convertView.findViewById(R.id.xbtime);
            viewHolder.xbworkName = (TextView) convertView.findViewById(R.id.xbworkName);
//            viewHolder.xbLayout = (RelativeLayout) convertView.findViewById(R.id.xbLayout);
//            viewHolder.xbData = (TextView) convertView.findViewById(R.id.xbData);

            convertView.setTag(viewHolder);




       } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (null!=mList.get(position).kqrq){
            viewHolder.topTimeLine.setText(mList.get(position).kqrq+"  "+setDataDay(mList.get(position).kqrq));
        }


        if (null == mList.get(position).bgsj) {//上班时间为空

//            viewHolder.sbLayout.setVisibility(View.GONE);
            viewHolder.sbcontent.setText("");
            viewHolder.sbtimes.setText("");
            viewHolder.sbworkName.setText("");
        } else {

//            viewHolder.sbLayout.setVisibility(View.VISIBLE);
//            viewHolder.sbData.setText(setDataDay(mList.get(position).bgsj));
            viewHolder.sbcontent.setText(mList.get(position).qddz);
            viewHolder.sbtimes.setText(mList.get(position).bgsj.substring(11,mList.get(position).bgsj.length()));
            viewHolder.sbworkName.setText("上班");

        }

        if (null == mList.get(position).zwbgsj) {//中午时间为空

//            viewHolder.zwLayout.setVisibility(View.GONE);
            viewHolder.zwcontent.setText("");
            viewHolder.zwtimes.setText("");
            viewHolder.zwworkName.setText("");

        } else {

//            viewHolder.zwLayout.setVisibility(View.VISIBLE);
//            viewHolder.zwData.setText(setDataDay(mList.get(position).zwbgsj));
            viewHolder.zwcontent.setText(mList.get(position).zwqddz);
            viewHolder.zwtimes.setText(mList.get(position).zwbgsj.substring(11,mList.get(position).zwbgsj.length()));
            viewHolder.zwworkName.setText("中午");

        }

        if (null == mList.get(position).xbbgsj) {//下班时间为空

//            viewHolder.xbLayout.setVisibility(View.GONE);
            viewHolder.xbcontent.setText("");
            viewHolder.xbtimes.setText("");
            viewHolder.xbworkName.setText("");
        } else {

//            viewHolder.xbLayout.setVisibility(View.VISIBLE);
            viewHolder.xbcontent.setText(mList.get(position).xbqtdz);
            viewHolder.xbtimes.setText(mList.get(position).xbbgsj.substring(11,mList.get(position).xbbgsj.length()));
            viewHolder.xbworkName.setText("下班");
//            viewHolder.xbData.setText(setDataDay(mList.get(position).xbbgsj));
        }
//          convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String sName = mList.get(position).name;
//                String mIds = mList.get(position).id;
//                String kehu_name = mList.get(position).name;
//                String kehu_id = mList.get(position).id;
//                Intent intent = new Intent();
//                intent.putExtra("Name", sName);
//                intent.putExtra("mId", mIds);
//                intent.putExtra("kehu_name", kehu_name);
//                intent.putExtra("kehu_id", kehu_id);
//                BusinCreateGroupPerson.instance.setResult(11, intent);
//                BusinCreateGroupPerson.instance.finish();
//            }
//        });
        return convertView;
    }

    public class ViewHolder {

        TextView topTimeLine;


//        RelativeLayout sbLayout;
        TextView sbcontent;
        TextView sbtimes;
        TextView sbworkName;
//        TextView sbData;

        TextView zwcontent;
        TextView zwtimes;
        TextView zwworkName;

        TextView xbcontent;
        TextView xbtimes;
        TextView xbworkName;
    }

    public String setDataDay(String data){
        final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                "星期六" };
        String s = data;
//        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        try {
            date = sdfInput.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if(dayOfWeek<0)dayOfWeek=0;
        System.out.println(dayNames[dayOfWeek]);
        return dayNames[dayOfWeek];
    }

}
